public class Soluton1 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode newHead = new ListNode(0); // 创建一个虚拟头结点，方便记录结果
        ListNode prev = newHead; // 尾插操作的尾指针
        int t = 0; // 记录进位

        while(cur1 != null || cur2 != null || t != 0) {
            // 先加上第一个链表
            if(cur1 != null) {
                t += cur1.val;
                cur1 = cur1.next;
            }
            // 再加上第二个链表
            if(cur2 != null) {
                t += cur2.val;
                cur2 = cur2.next;
            }
            prev.next = new ListNode(t % 10);
            prev = prev.next;
            t /= 10;
        }

        return newHead.next;
    }
}
